"""
url请求路径中，不允许出现中文，需要unicode编码
"""
import urllib.request
import urllib.parse

from urllib3 import HTTPResponse

# 把url中的汉字编码Unicode

# # 单个字符：
# name = urllib.parse.quote("周杰伦")
# print(name)
# url = f"https://www.baidu.com/s?wd={name}"

# 多个字符：
data = {
    "wd": "周杰伦",
    "sex": "男"
}
data = urllib.parse.urlencode(data)
print(data)  # wd=%E5%91%A8%E6%9D%B0%E4%BC%A6&sex=%E7%94%B7
url = f"https://www.baidu.com/s?{data}"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.3.5211 SLBChan/105"
}
my_request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(my_request)
content = response.read().decode("utf-8")
print(content)
